package com.chuku.mapper;

import com.bean.WmsTBill;
import com.bean.WmsTBillDetails;
import com.bean.WmsTCustomer;
import com.bean.WmsTStore;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface ChukuMapper {


    @Select("select * from wms_t_customer where custName like '%${custName}%'")
    public List<WmsTCustomer> getManyHuozhu(@Param("custName") String custName);

    @Select("select * from wms_t_bill_details where billID in (select billID from wms_t_bill where customerID = #{custId} and billType='0' and billState='3' and billisDel='0')")
    public List<WmsTBillDetails> getManyMingxi(@Param("custId") String customerId);

    //根据trayId修改trayState
    @Select("update wms_t_tray set trayState='0' where trayID=#{value}")
    public void accordingTrayidByTraystate(String trayId);

    @Insert("insert into wms_t_bill (billID,billTransactor,customerID,billState,billDate,billType,billNote,billFeeStore" +
            ",billFeeOther,billPayable,billPaid,billEditTime,billisDel) values(#{billId},#{billTransactor},#{customerId},#{billState},#{billDate},#{billType},#{billNote}" +
            ",#{billFeeStore},#{billFeeOther},#{billPayable},#{billPaid},#{billEditTime},'1')")
    public void addMingxi(WmsTBill wmsTBill);


    @Select("select * from wms_t_store where storeID in\n" +
            "(select storeID from wms_t_site where siteID in\n" +
            "(select siteID from wms_t_tray where trayID in \n" +
            "(select trayID from wms_t_tray where trayState='1')))")
    public List<WmsTStore> getManyCangkuName();

    //当点击结算费用的按钮时向WmsTBillDetails表添加数据
    @Insert("INSERT into wms_t_bill_details VALUES(#{detailsId},#{billId},#{trayId},#{detailsAffirmant},#{detailsConservator},#{detailsGoodsName},#{detailsEstiTotal},#{detailsGoodsNum},#{detailsGoodsSpec},#{currentGoodsNum},#{detailsEditTime},#{detailsIsDel})")
    public void insert2(WmsTBillDetails ad);


    //根据仓库id获取所有的数据
    @Select("select wms_t_bill.billID,wms_t_customer.custName,count(*)as count,wms_t_store.storeName,wms_t_bill.billDate,wms_t_bill.billNote,wms_t_bill.billIsDel from wms_t_bill \n" +
            "LEFT JOIN wms_t_bill_details on wms_t_bill.billID = wms_t_bill_details.billID\n" +
            "LEFT JOIN wms_t_tray on wms_t_tray.trayID = wms_t_bill_details.trayID\n" +
            "LEFT JOIN wms_t_site on wms_t_site.siteID = wms_t_tray.siteID\n" +
            "LEFT JOIN wms_t_store on wms_t_site.storeID = wms_t_store.storeID \n" +
            "LEFT JOIN wms_t_customer on wms_t_bill.customerID = wms_t_customer.custID where wms_t_store.storeID=#{value} GROUP BY wms_t_bill_details.billID  ")
    public List<Map<String,Object>> getAll(String storeId);



    //根据出库时间段  出库单号  货主名  【动态查询】出库单号  结算员  货主 业务时间  应收款  实际收款  库存费
    @Select("<script>select b.billID,b.billTransactor,c.custName,b.billDate,b.billPayable,b.billPaid,billFeeOther\n" +
            "            from wms_t_bill as b LEFT JOIN wms_t_customer as c on\n" +
            "            b.customerID = c.custID\n" +
            "        where b.billType='1' and b.billState='0' and b.billIsDel='0'\n" +
            "        <if test=\"time1!='' and time2!=''\">\n" +
            "            and b.billDate between #{time1} and #{time2}\n" +
            "        </if>\n" +
            "        <if test=\"billId!=''\">\n" +
            "            and b.billId = #{billId}\n" +
            "        </if>\n" +
            "       <if test=\"custName!=''\">\n" +
            "            and c.custName = #{custName}\n" +
            "            </if></script>")
    public List<Map<String, Object>> selectChukujilu(@Param("billId") String billId, @Param("custName") String custName, @Param("time1") String time1, @Param("time2") String time2);

    //根据出库时间段  出库单号  货主名  【动态查询】明细号  货主 托盘号  货物名称  验货员 仓管员 入库数量
    @Select("<script>select  d.detailsID,c.custName,d.trayID,d.detailsGoodsName,d.detailsAffirmant,d.detailsConservator,d.detailsGoodsNum\n" +
            " from wms_t_bill_details as d LEFT JOIN  wms_t_bill as b on d.billID = b.billID LEFT JOIN wms_t_customer as c on\n" +
            "b.customerID = c.custID\n" +
            "        where b.billType='1' and b.billState='0' and b.billIsDel='0'\n" +
            "        <if test=\"time1!='' and time2!=''\">\n" +
            "            and b.billDate between #{time1} and #{time2}\n" +
            "        </if>\n" +
            "        <if test=\"billId!=''\">\n" +
            "            and b.billId = #{billId}\n" +
            "        </if>\n" +
            "       <if test=\"custName!=''\">\n" +
            "            and c.custName = #{custName}\n" +
            "            </if></script>")
    public List<Map<String, Object>> selectChukumingxi(@Param("billId") String billId, @Param("custName") String custName, @Param("time1") String time1, @Param("time2") String time2);
}
